//
//  TFGoodsViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "TFGoodsViewController.h"
#import "YKClientListHeaderView.h"
#import "YKSaleItem.h"
#import "YKSearchNormalDataByGoodViewController.h"
@interface TFGoodsViewController ()

@end

@implementation TFGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    YKClientListHeaderView* listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"通用名",@"销量",@"金额"] weights:@[@1,@1,@1]];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
        
    }];
    listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    UIButton *confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"查询明细" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [confirmButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.view addSubview:confirmButton];
    [confirmButton addTarget:self action:@selector(onSearchDetailTapped:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
        make.bottom.equalTo(confirmButton.mas_top);
    }];
    
    [self loadData];
}

- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"manage/normalSummary.htm" parameters:@{@"startDate":[NSString stringWithFormat:@"%zd-01-01",[[NSDate date]tf_year]], @"endDate":[[NSDate date] tf_dateStringWithFormat:@"yyyy-MM-dd"]} success:^(id responseObject) {
        
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
        [self.tableViewManager removeAllSections];
        NSArray *normalSummary = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"normalSummary"]];
        
        TFTableViewSection * section = [[TFTableViewSection alloc]init];
        section.headerHeight = .1f;
        section.footerHeight = .1f;
        [section addItemsFromArray:[normalSummary tf_mapUsingBlock:^id _Nullable(id  _Nonnull obj, NSInteger idx) {
            return [YKSaleItem itemWithModel:obj];
        }]];
        
        
        if (section.items.count == 0) {
            [self tf_showStateView:kTFViewStateNoData inView:self.tableView];
        }
        [self.tableViewManager addSection:section];
        
        [self.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}

- (void)onSearchDetailTapped:(id)sender {
        YKSearchNormalDataByGoodViewController *vc = [[YKSearchNormalDataByGoodViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
}
@end
